package DataStructure.Queue;

import org.ietf.jgss.Oid;

/**
 * @Author：DijaH
 * @Date：2023/2/28 9:00 下午
 * @Version：1.0.0
 * @Description： 普通数组实现队列
 */
public class ArrayQueen {

    private Integer[] arr;
    private Integer size;
    private Integer first;
    private Integer last;

    //构造函数
    public ArrayQueen(int initSize){
        if(size < 0){
            throw new IllegalArgumentException("the initsize less than 0 ");
        }

        arr = new Integer[initSize];
        size = 0;
        first = 0;
        last = 0;
    }

    public Integer peek(){
        if(size == 0){
            //没有元素了
            return null;
        }
        return arr[first];
    }

    public void push(int num){
        if(size == arr.length){
            throw new ArrayIndexOutOfBoundsException("the queen is full");
        }
        size++;
        arr[last] = num;
        last = last == arr.length - 1 ? 0 : last + 1;
    }

    public int poll(){
        if(size == 0){
            throw new ArrayIndexOutOfBoundsException("this queue is empty");
        }
        size--;
        int temp = first;
        first = first == arr.length - 1 ? 0 : first + 1;
        return arr[temp];
    }
}
